﻿using System;
using System.Collections.Generic;
using System.Globalization;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Framework;
using DotNetNuke.Services.FileSystem;


namespace Aricie.DNN.Services.Url.FriendlyUrl.Providers.DNN6
{
    public class StandardHtmlFilterUrlRewriter : IUrlRewriterProvider, IIsActive
    {

        IFileManager _FileManager = FileManager.Instance;

        public List<GroupParamsRewrite> GetRewrites()
        {
            List<GroupParamsRewrite> toReturn = new List<GroupParamsRewrite>();
            string desc = "Original path of the file being requested with an encrypted ticket";
            toReturn.Add(new GroupParamsRewrite("DnnFilePath", desc, RewriteType.PageName,  UrlParam.FromSyntax("fileticket")));
            toReturn[toReturn.Count - 1].UrlParams[UrlParam.FromSyntax("fileticket")] = ParamNature.NonExclusive | ParamNature.Mandatory;
            desc = "depending on the file, this is, relative to the application path, the path to the host default directory or the portal default directory";
            toReturn.Add(new GroupParamsRewrite("DnnFileRootPath", desc, RewriteType.SubPath, UrlParam.FromSyntax("fileticket")));
            toReturn[toReturn.Count-1].UrlParams[UrlParam.FromSyntax("fileticket")] = ParamNature.NonExclusive | ParamNature.Mandatory;
            desc = "this is the path of the file's directory, relative to the file's root path";
            toReturn.Add(new GroupParamsRewrite("DnnFileFolder", desc, RewriteType.SubPath, UrlParam.FromSyntax("fileticket")));
            toReturn[toReturn.Count - 1].UrlParams[UrlParam.FromSyntax("fileticket")] = ParamNature.NonExclusive | ParamNature.Mandatory;
            desc = "this is the filename, minus the extension";
            toReturn.Add(new GroupParamsRewrite("DnnFileExtensionLessFileName", desc, RewriteType.SubPath, UrlParam.FromSyntax("fileticket")));
            toReturn[toReturn.Count - 1].UrlParams[UrlParam.FromSyntax("fileticket")] = ParamNature.NonExclusive | ParamNature.Mandatory;
            desc = "this is the file extension";
            toReturn.Add(new GroupParamsRewrite("DnnFileExtension", desc, RewriteType.SubPath, UrlParam.FromSyntax("fileticket")));
            toReturn[toReturn.Count - 1].UrlParams[UrlParam.FromSyntax("fileticket")] = ParamNature.NonExclusive | ParamNature.Mandatory;

            return toReturn;
        }

        public GroupRewriteResult RewriteParams(string groupName, RewriteType objRewriteType, Dictionary<UrlParam, string> @params,
            FriendlierUrlStrategy behaviour)
        {
            GroupRewriteResult toReturn = new GroupRewriteResult();

            if (@params.Count > 0)
            {
                string fileTicket = @params[UrlParam.FromSyntax("fileticket")];
                var fileId = int.Parse(UrlUtils.DecryptParameter(fileTicket), CultureInfo.InvariantCulture);
                
                var objFile = _FileManager.GetFile(fileId);
                switch (groupName.ToLowerInvariant())
                {
                    case "dnnfilepath":
                        toReturn.RewriteValue = GetFilePath(objFile);
                        toReturn.ConsumedParameters[UrlParam.FromSyntax("fileticket")] = true;
                        break;
                    case "dnnfilerootpath":
                        toReturn.RewriteValue = GetFileRootPath(objFile);
                        toReturn.ConsumedParameters[UrlParam.FromSyntax("fileticket")] = true;
                        toReturn.ConsumedParameters[UrlParam.PortalId] = true;
                        break;
                    case "dnnfilefolder":
                        toReturn.RewriteValue = objFile.Folder.Trim('/');
                        toReturn.ConsumedParameters[UrlParam.FromSyntax("fileticket")] = true;
                        break;
                    case "dnnfileextensionlessfilename":
                        toReturn.RewriteValue = objFile.FileName.Replace("." + objFile.Extension, "");
                        toReturn.ConsumedParameters[UrlParam.FromSyntax("fileticket")] = true;
                        break;
                    case "dnnfileextension":
                        toReturn.RewriteValue = objFile.Extension;
                        toReturn.ConsumedParameters[UrlParam.FromSyntax("fileticket")] = true;
                        break;

                }
            }
            return toReturn;
        }


        private string GetFileRootPath(IFileInfo objFile)
        {
            var toreturn = (objFile.PortalId == Null.NullInteger ? Globals.HostPath : new PortalSettings(objFile.PortalId).HomeDirectory) ;
            return toreturn.Substring((Globals.ApplicationPath.TrimEnd('/') + "/Portals").Length).Trim('/');
        }

        private string GetFilePath(IFileInfo objFile)
        {
            var toreturn = _FileManager.GetUrl(objFile);
            return toreturn.Substring(Globals.ApplicationPath.Length).Trim('/');
        }

        public bool IsActive => true;
    }
}